package ee.android.kinokavad.fragments;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;

import ee.android.kinokavad.App;
import ee.android.kinokavad.R;
import ee.android.kinokavad.adapters.MovieSessionAdapter;
import ee.android.kinokavad.data.Cinema;
import ee.android.kinokavad.data.Movie;
import ee.android.kinokavad.data.Session;

public class MovieSessionFragment extends SherlockFragment{

	public static Fragment newInstance(Bundle args) {
		Fragment f = new MovieSessionFragment();
		f.setArguments(args);
		return f;
	}

	public static MovieSessionFragment newInstance(long movieId, boolean selectedCinemas) {
		MovieSessionFragment f = new MovieSessionFragment();

		Bundle args = new Bundle();
		args.putLong("movieId", movieId);
		args.putBoolean("selectedCinemas", selectedCinemas);
		f.setArguments(args);

		return f;
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		return inflater.inflate(R.layout.movie_session_fragment, container, false);

	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		getSherlockActivity().getSupportActionBar().setSubtitle("Seansid");
		initList(getArguments().getLong("movieId"), getArguments().getBoolean("selectedCinemas"));
	}


	private void initList(long movieId, boolean selectedCinemas) {

		App kinokavad = (App) getActivity().getApplicationContext();
		kinokavad.getDataController().open();
		List<Session> sessions = kinokavad.getDataController().loadSessionsForMovie(movieId);
		List<Cinema> cinemas = kinokavad.getDataController().loadAllCinemasFromDatabase();
		Movie movie = kinokavad.getDataController().loadMovieByNetId(movieId);
		kinokavad.getDataController().closeRequest();

		List<String> dates = new ArrayList<String>();
		List<ArrayList<Session>> groupedSessions = new ArrayList<ArrayList<Session>>();

		for (Session session : sessions) {

			for (Cinema cinema : cinemas) {
				if (cinema.getNetId() == session.getCinemaId() && (cinema.isSelected() || !selectedCinemas)) {
					session.setCinemaName(cinema.getName());
					break;
				}
			}
			if (session.getCinemaName() == null) continue;
			DateTime date = new DateTime(session.getDateTime());
			String day = MainFragmentKinoKavad.DAYS[date.getDayOfWeek()-1];
			String dateString = day + ", " + date.toString("dd.MM");

			if (!dates.contains(dateString)) {
				dates.add(dateString);
				groupedSessions.add(new ArrayList<Session>());
			}
			int index = dates.indexOf(dateString);
			groupedSessions.get(index).add(session);
		}
		
		ExpandableListView list = (ExpandableListView) getView().findViewById(R.id.movieSessionList);

		MovieSessionAdapter adapter = new MovieSessionAdapter(getActivity(), dates, groupedSessions);
		list.setAdapter(adapter);
		((TextView) getView().findViewById(R.id.movieSessionTitle)).setText(movie.getTitle());
		if (adapter.getGroupCount() == 0) {
			View empty = getView().findViewById(R.id.movieSessionEmpty);
			empty.setVisibility(View.VISIBLE);
			empty.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					getChildFragmentManager().popBackStack();
				}
			});
		} else {
			list.expandGroup(0);
		}
	}


}
